package org.gangcai.mac.shop.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.common.MultiStatusActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class AgentActivity extends MultiStatusActivity {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.identifyNumber)
    EditText identifyNumber;
    @BindView(R.id.address)
    EditText address;
    @BindView(R.id.nextStep)
    Button nextStep;
    @BindView(R.id.weixin)
    EditText weixin;
    @BindView(R.id.message)
    EditText message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initTitleBarView(titlebar, "申请成为中介");
    }

    @Override
    public int getContentView() {
        return R.layout.activity_agent;
    }

    @OnClick(R.id.nextStep)
    public void onViewClicked() {
        if (TextUtils.isEmpty(username.getText().toString())) {
            ToastUtils.showShort("请输入的您的姓名");
        } else if (TextUtils.isEmpty(identifyNumber.getText().toString())) {
            ToastUtils.showShort("请输入您的电话号码");
        } else if (TextUtils.isEmpty(address.getText().toString())) {
            ToastUtils.showShort("请输入您的QQ号码");
        } else if (TextUtils.isEmpty(weixin.getText().toString())) {
            ToastUtils.showShort("请输入您的微信号");
        } else {
            submitData();
        }
    }

    private void submitData() {
        prompDialog.showLoading("请等待");
        RetrofitManager.create().apply_intermediary(SPUtils.getInstance().getString("uid"), username.getText().toString(), identifyNumber.getText().toString(), address.getText().toString(), weixin.getText().toString(), message.getText().toString())
                .compose(this.<CommonResonseBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            prompDialog.showSuccess("提交成功");
                            ToastUtils.showShort("我们将在近期联系你,请保持电话畅通");
                            finish();
                        }
                    }
                });

    }
}
